package com.mmp.main.servlet.device;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDeviceScrappingInfo;
import com.mmp.main.entity.LookupScrappingReason;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class DeviceScrappingProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int deviceId = WebUtil.getParamValueAsInteger(request, "deviceId", 0);

		if (deviceId == 0) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		int deviceScrappingId = WebUtil.getParamValueAsInteger(request, "deviceScrappingId", 0);

		HospitalDeviceScrappingInfo scrapping = null;
		if (deviceScrappingId != 0)
			scrapping = dbQueryManager.findById(deviceScrappingId, HospitalDeviceScrappingInfo.class);
		else
			scrapping = new HospitalDeviceScrappingInfo();

		HospitalDevice device = dbQueryManager.findById(deviceId, HospitalDevice.class);
		device.setStatus(Enums.DEVICE_STATUS.SCRAPPED.getStatus());
		scrapping.setHospitalDevice(device);

		scrapping.setDeviceCondition(WebUtil.getParamValue(request, "deviceCondition", null));

		int scrappingReason = WebUtil.getParamValueAsInteger(request, "scrappingReason", 0);
		if (scrappingReason != 0) {
			LookupScrappingReason reason = new LookupScrappingReason();
			reason.setId(scrappingReason);
			scrapping.setScrappingReason(reason);
		}

		String[] scrappingReasonReportUrl = FileUtil.uploadFiles(request, "scrappingReasonReportUrl");
		// if (scrappingReasonReportUrl != null &&
		// scrappingReasonReportUrl.length != 0)
		scrapping.setScrappingReasonReportUrl(Common.concatenateValues(scrappingReasonReportUrl));

		String[] scrappingFinalReportUrl = FileUtil.uploadFiles(request, "scrappingFinalReportUrl");
		// if (scrappingFinalReportUrl != null && scrappingFinalReportUrl.length
		// != 0)
		scrapping.setScrappingFinalReportUrl(Common.concatenateValues(scrappingFinalReportUrl));

		scrapping.setScrappingDate(WebUtil.getParamValueAsDate(request, "scrappingDate", null));

		scrapping.setCreatedBy(getSessionUser(request));

		scrapping.setCreatedIn(new Date());

		dbQueryManager.mergeEntity(scrapping);

		if (deviceScrappingId == 0)
			setMessage(request, new Message("تم التكهين بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		else
			setMessage(request, new Message("تم التعديل بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		sendRedirect(request, response, "ScrappDeviceSearch");
	}
}
